package com.ve.downloader.utils;


/**
 * @description: 操作顺序都是高位在前，低位在后，
 */
public class BinUtils {


    public static void main(String[] args) {
        byte b = 6;
        print(b);
        b = setBit(b, 2, 1);
        print(b);
        b = setBit(b, 5, 0);
        print(b);
        for (int j = 0; j < 8; j++) {
            System.out.print(" "+getBit(b,j));
        }
        System.out.println();
    }

    public static void print(short b) {
        for (int i = 0; i < Short.SIZE; i++) {
            System.out.print(((b & (1 << (Byte.SIZE - i - 1))) != 0 ? 1 : 0) + " ");
        }
        System.out.println();
    }

    public static void print(byte b) {
        for (int i = 0; i < Byte.SIZE; i++) {
            System.out.print(((b & (1 << (Byte.SIZE - i - 1))) != 0 ? 1 : 0) + " ");
        }
        System.out.println();
    }


    //-------------------

    public static byte setBit(byte data, int index, boolean value) {
        int mode = (1 << (7 - index)) & 0xff;
        return (byte) (value ? (data | mode) : (data & (~mode)));
    }

    public static byte setBit(byte data, int index, int value) {
        return setBit(data, index, value == 1);
    }

    public static boolean getBit(byte b, int index) {
        return (b & (1 << (7 - index))) != 0;
    }

    public static byte bits2byte(byte[] bits) {

        byte res = 0;
        for (int i = 0; i < Byte.SIZE; i++) {
            res |= (bits[i] == 1) ? 1 << (Byte.SIZE - i - 1) : 0;
        }
        return res;
    }


    public static byte[] byte2bits(byte b) {
        byte[] bits = new byte[Byte.SIZE];
        for (int i = 0; i < bits.length; i++) {
            bits[(bits.length - 1) - i] = (byte) ((b & (1 << i)) != 0 ? 1 : 0);
        }
        return bits;
    }

    public static boolean[] byte2bitsb(byte b) {
        boolean[] bits = new boolean[Byte.SIZE];
        for (int i = 0; i < bits.length; i++) {
            bits[(bits.length - 1) - i] = (b & (1 << i)) != 0;
        }
        return bits;
    }

    public static int bytes2int16(byte high, byte low) {
        int result = 0;
        result |= high & 0xff;
        result <<= 8;
        result |= low & 0xff;
        return result;
    }


    public static int bytes2int12(byte high, byte low) {
        return bytes2int16(high, low) >> 4;
    }

    public static byte[] int2bytes12(int value) {
        byte[] bytes = new byte[2];//hight,low
        bytes[1] = (byte) (value << 4 & 0xff);//low
        bytes[0] = (byte) (value >> 4 & 0xff);
        return bytes;
    }

    public static float int2float12(int intValue, float min, float max) {
        return ((1.0f * intValue) / 4096f) * (max - min) + min;
    }

    public static byte[] float2bytes12(float floatValue, float min, float max) {
        int value = (int) (((floatValue - min) * 4096) / (max - min));//float2int12
        return int2bytes12((value));
    }

    public static byte bool2yte(boolean selected) {
        return (selected ? 0x01 : (byte) 0x00);
    }


}
